package com.recommender.controller;

import com.recommender.entity.MovieInfo;
import com.recommender.entity.Movies;
import com.recommender.service.MovieInfoServiceImpl;
import com.recommender.service.MoviesServiceImpl;
import com.recommender.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MovieController {
    @Autowired
    private MovieInfoServiceImpl movieInfoService;
    @Autowired
    private MoviesServiceImpl moviesService;
    @RequestMapping("/detail/movieId={movieId}/recom={recom}")
    public String getDeatil(@PathVariable("movieId")int id, @PathVariable("recom")String[] recom, HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        MovieInfo movieInfo = movieInfoService.queryById(id);
//        System.out.println(Arrays.toString(recom));
        ArrayList<Integer> recomList = new ArrayList<>();
        for (String s : recom) {
            recomList.add(Integer.parseInt(s));
        }
        ArrayList<MovieInfo> recommendMovies = new ArrayList<>();
        for (Integer recomId : recomList) {
            MovieInfo recomInfo = movieInfoService.queryById(recomId);
            if(recomInfo!=null) recommendMovies.add(recomInfo);
        }
//        for (MovieInfo movie : recommendMovies) {
//            System.out.println(movie);
//        }
        model.addAttribute("movieDetail", movieInfo);
        model.addAttribute("recommendMovies",recommendMovies);
        return "detail";
    }
    @RequestMapping({"/","/index","/index.html"})
    public String index(Model model, @RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "toPage",required = false) Integer toPage){
        if(pageNum==null) pageNum=1;
        int pageCapbility=24;
        PageInfo pageInfo = new PageInfo(pageNum, pageCapbility,moviesService.getCount());
        if(toPage!=null && toPage>0 && toPage<=pageInfo.getTotalPage()) pageInfo.pageNum=toPage;
        List<Movies> querryedMovies = moviesService.queryAllByLimit((pageInfo.pageNum-1)*pageCapbility, pageCapbility);
        List<MovieInfo> movies=new ArrayList<>();
        for (Movies movie : querryedMovies) {
            movies.add(movieInfoService.queryById(movie.getId()));
        }
//        for (MovieInfo movie : movies) {
//            System.out.println(movie);
//        }
        boolean hasMoreToShow = movies.size() > 0;
//        System.out.println(hasMoreToShow);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("movies",movies);
        model.addAttribute("hasMoreToShow",hasMoreToShow);
        return "index";
    }
    @RequestMapping("/about")
    public String about(){
        return "about";
    }
    @RequestMapping("/contact")
    public String contact(){
        return "contact";
    }
}
